﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.SalesCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.SalesCommandHandler
{
    /// <summary>
    /// 退货通知单的添加命令处理方法
    /// </summary>
    public class Addreturn_notice_mainCommandHandler : IRequestHandler<Addreturn_notice_mainCommand, APIResult<object>>
    {

        private readonly IMapper mapper;
        /// <summary>
        /// 退货通知单的仓储接口
        /// </summary>
        private readonly IBaseRepository<return_notice_main> rRepo;
        /// <summary>
        /// 订单物料的仓储接口
        /// </summary>
        private readonly IBaseRepository<OrderMaterials> materialBiz;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="rRepo"></param>
        /// <param name="materialBiz"></param>
        public Addreturn_notice_mainCommandHandler(IMapper mapper, IBaseRepository<return_notice_main> rRepo , IBaseRepository<OrderMaterials> materialBiz)
        {
            this.mapper = mapper;
            this.rRepo = rRepo;
            this.materialBiz = materialBiz;
        }
        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<object>> Handle(Addreturn_notice_mainCommand request, CancellationToken cancellationToken)
        {

            APIResult<object> result = new APIResult<object>();
            return_notice_main r = new return_notice_main();
            r.document_code = request.document_code;
            r.document_date = request.document_date;
            r.business_type_id = request.business_type_id;
            r.warehouse_id = request.warehouse_id;
            r.operators = request.operators;
            r.customer_name = request.customer_name;
            r.document_status = request.document_status;
            r.auditor = request.auditor;
            r.audit_date = request.audit_date;
            r.business_status_id = request.business_status_id;
            r.return_reason_code = request.return_reason_code;
            r.handling_method_code = request.handling_method_code;
            r.remark = request.remark;
            r.ShenPiStatus = "待审核";
            int add1 = rRepo.Add(r).Result;
            if (add1 > 0)
            {
                request.OrderMaterials.ForEach(x =>
                {
                    x.SaleCode = r.document_code;
                });
                int add2 = materialBiz.AddRanges(request.OrderMaterials).Result;
                if (add2 > 0)
                {
                    result.Code = APIEnum.添加成功;
                    result.Msg = APIEnum.添加成功.ToString();
                }
                else
                {
                    result.Code = APIEnum.添加失败;
                    result.Msg = APIEnum.添加失败.ToString();
                }
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
            }
            return Task.FromResult(result);


        }



    }


}
